package model.dao;

import java.util.ArrayList;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import model.bean.Ambiente;
import model.bean.Evento;
import model.bean.Participante;
import model.interfaces.IEvento;

public class EventoDAO implements IEvento {

	public EntityManager getEntitymanager(){
		EntityManagerFactory factory = null;
		EntityManager manager = null;
		
		try{
			factory = Persistence.createEntityManagerFactory("SmartClassroomPUnit");
			manager = factory.createEntityManager();
		}catch(Exception e){
			System.out.println("Erro : " + e);
		}finally{
			factory.close();
		}
		
		return manager;
	}

	@Override
	public void insereParticipanteEmEvento(Participante participante , Evento evento) {
		EntityManager manager = getEntitymanager();
		try{
			manager.getTransaction().begin();
			System.out.println("Linkando participante ao Evento...");
			evento.getParticipantes().add(participante);
			manager.persist(evento);
			manager.getTransaction().commit();
		}catch(Exception e){
			System.out.println("Erro : " + e);
		}finally{
			manager.close();
		}
	}

	@Override
	public void vinculaAmbienteEvento(Ambiente ambiente , Evento evento) {
		EntityManager manager = getEntitymanager();
		try{
			manager.getTransaction().begin();
			System.out.println("Linkando Ambiente ao Evento...");
			evento.getAmbientes().add(ambiente);
			manager.persist(evento);
			manager.getTransaction().commit();
		}catch(Exception e){
			System.out.println("Erro : " + e);
		}finally{
			manager.close();
		}
	}


	public String getDescricao(Evento evento) {
		return evento.getDescricao();

	}

	@Override
	public void cadastraEvento(Evento evento) {
		EntityManager manager = getEntitymanager();
		try{
			manager.getTransaction().begin();
			System.out.println("Cadastrando Evento " + evento.getDescricao());
			manager.persist(evento);
			manager.getTransaction().commit();
		}catch(Exception e){
			System.out.println("Erro : " + e);
		}finally{
			manager.close();
		}
	}

	@Override
	public ArrayList<Participante> getParticipantes(Evento evento) {
		return evento.getParticipantes();
	}
}
